package com.noble.glamour.module.local.msgview.chatview.msgpanel;

import android.app.Activity;
import android.content.Context;
import android.widget.ImageView;
import android.widget.TextView;

import com.noble.glamour.R;
import com.noble.glamour.bean.center.user.light.UserLight;
import com.noble.glamour.module.local.chat.msgtype.bean.BaseMessage;
import com.noble.glamour.module.local.chat.msgtype.type.VideoMessage;
import com.noble.glamour.module.local.msgview.ChatAdapter;
import com.noble.glamour.module.local.msgview.chatview.ChatPanel;
import com.noble.glamour.module.logic.application.App;
import com.noble.glamour.module.logic.application.ModuleMgr;
import com.noble.glamour.ui.agora.model.AgoraConstant;
import com.noble.library.log.PLogger;
import com.noble.library.view.CustomFrameLayout;

/**
 * 音视频结束后状态展示panel
 * Created by Kind on 2017/5/12.
 */
public class ChatPanelVideo extends ChatPanel {

    private ImageView chat_item_type_img;
    private TextView chat_item_video_text;
    private UserLight infoLightweight;

    public ChatPanelVideo(Context context, ChatAdapter.ChatInstance chatInstance, boolean sender) {
        super(context, chatInstance, R.layout.f1_chat_item_panel_audio, sender);
    }

    @Override
    public void initView() {
        CustomFrameLayout fl_video_panel = (CustomFrameLayout) findViewById(R.id.fl_video_panel);
        if (isSender()) {
            chat_item_type_img = (ImageView) findViewById(R.id.iv_type_right);
            chat_item_video_text = (TextView) findViewById(R.id.tv_text_right);
            fl_video_panel.showOfIndex(1);
        } else {
            chat_item_type_img = (ImageView) findViewById(R.id.iv_type_left);
            chat_item_video_text = (TextView) findViewById(R.id.tv_text_left);
            fl_video_panel.showOfIndex(0);
        }
    }

    @Override
    public boolean reset(BaseMessage msgData, UserLight infoLightweight) {
        if (msgData == null || !(msgData instanceof VideoMessage)) return false;
        this.infoLightweight = infoLightweight;

        VideoMessage videoMessage = (VideoMessage) msgData;
        chat_item_type_img.setImageResource(videoMessage.isVideoMediaTp()
                ? (isSender() ? R.drawable.f1_chat_video_right : R.drawable.f1_chat_video_left)
                : (isSender() ? R.drawable.f1_chat_voice_right : R.drawable.f1_chat_voice_left));
        chat_item_video_text.setText(VideoMessage.getVideoChatContent(
                videoMessage.getEmLastStatus(), videoMessage.getVideoVcTalkTime(), videoMessage.isSender()));

        PLogger.d("tp=" + videoMessage.getVideoTp() + ":code=" + videoMessage.getVideoVcEscCode());
        return true;
    }

    @Override
    public boolean onClickContent(final BaseMessage msgData, boolean longClick) {
        if (msgData == null || !(msgData instanceof VideoMessage)) return false;

        // 如果是别人发起的音视频，点击之后更新小红点已读状态
        if (!isSender()) ModuleMgr.getChatMgr().updateMsgFStatus(msgData.getMsgID(), null);
        // 发起音视频，点击自己或他人的视频通话消息均调起与对方通话[2017.8.13/16:50]
        if (((VideoMessage) msgData).isVideoMediaTp())
            ModuleMgr.getRtcChatMgr().inviteVideo((Activity) App.getActivity(), msgData.getLWhisperID(),
                    AgoraConstant.APPEAR_TYPE_NO, "");
        else
            ModuleMgr.getRtcChatMgr().inviteVoice((Activity) App.getActivity(), msgData.getLWhisperID(),
                    "");

        return true;
    }
}
